<template>
  <div>
    <div class="action-bar">
      <el-button v-if="canAdd" type="primary" size="mini" icon="el-icon-plus" @click="add">新建任务</el-button>
      <el-upload
        :headers="headers"
        :action="uploadMyPanUrl"
        :before-upload="handleBeforeUpLoad"
        :on-success="handleSuccess"
        :on-error="handleError"
        :show-file-list="false"
      >
        <el-button size="mini" plain icon="el-icon-upload2">上传离线盘点文件</el-button>
      </el-upload>
      <el-button type="primary" size="mini" icon="el-icon-download" @click="download">下载离线盘点文件</el-button>
    </div>
    <FormDialog v-if="dialogFormVisible" :loading="loading" title="新建任务" width="80%" :show="dialogFormVisible" :append-to-body="true" @on-ok="ok" @on-cancel="cancel">
      <EditForm ref="editForm" :plan-id="planId" :belongs="$attrs.belongs" />
    </FormDialog>
  </div>
</template>
<script>
import FormDialog from '@neu/biz-core/components/FormDialog'
// 编辑实例组件独立编写，需要传递id属性，判断新建还是更新操作
import EditForm from './editForm'

import { remove as removeTem } from '@neu/dcam-ui/api/inventoryResultkj'
import {
  downloadMyPan,
  uploadMyPanUrl
} from '@neu/dcam-ui/api/inventoryDuty'
import { getToken } from '@neu/biz-core/utils/auth'
import { detail as planDetail } from '@neu/dcam-ui/api/inventoryPlan'

export default {
  name: 'InventoryDutyActionBar',
  components: {
    FormDialog,
    EditForm
  },
  props: {
    refresh: {
      required: true,
      type: Function
    },
    planId: {
      required: true,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      loading: true,
      dialogFormVisible: false,
      status: this.$route.query.status ? this.$route.query.status : '',
      uploadMyPanUrl: uploadMyPanUrl,
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Authentication': getToken()
      },
      canAdd: false
    }
  },
  created() {
    if (this.planId > -1) {
      planDetail(this.planId).then(planInfo => {
        if (planInfo.data.status === 'created') {
          this.canAdd = true
        }
      })
    }
  },
  methods: {
    // 新建用户按钮点击处理
    add() {
      this.dialogFormVisible = true
    },
    // 创建用户对话框确认事件处理
    ok() {
      // 直接调用表单实例的保存方法
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      this.$refs.editForm.save().then(response => {
        loading.close()
        if (response === false) return
        // this.$message({
        //   showClose: true,
        //   message: '任务已经提交，后台会异步构建盘点任务，请稍后刷新查看结果！',
        //   type: 'success'
        // })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.refresh()
        // 关闭对话框
        this.dialogFormVisible = false
        this.$refs.editForm.reset()
      })
    },
    cancel() {
      this.$refs.editForm.reset()
      this.dialogFormVisible = false
      removeTem(-1).then(res => {
        console.log('清空成功')
      })
    },
    // 返回计划列表
    back() {
      // this.$router.push({ name: 'dcam_inventoryPlan' })
      this.$router.back()
    },
    handleBeforeUpLoad(file) {
      if (file.name !== 'pan.pd') {
        this.$message({
          showClose: true,
          message: '请上传 pan.pd 文件',
          type: 'error'
        })
      }
    },
    handleSuccess(response, file, fileList) {
      if (response.status === 'failure') {
        this.$message({
          showClose: true,
          message: response.data,
          type: 'error'
        })
      } else {
        this.$message({
          showClose: true,
          message: '数据上传成功',
          type: 'success'
        })
        this.refresh()
      }
    },
    handleError() { },
    download() {
      downloadMyPan().then(res => {
        const content = res.data
        const blob = new Blob([content])
        const fileName = 'pan.pd'
        if ('download' in document.createElement('a')) { // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else { // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
      })
    }
  }

}
</script>
